package cn.com.hhrcw.login;

import cn.com.hhrcw.entity.EnInfo;
import cn.com.hhrcw.entity.PiInfo;
import cn.com.hhrcw.entity.SysUser;
import cn.com.hhrcw.entity.SysUserDepart;
import cn.com.hhrcw.service.IEnInfoService;
import cn.com.hhrcw.service.IPiInfoService;
import cn.com.hhrcw.service.ISysUserDepartService;
import cn.com.hhrcw.service.ISysUserService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.encryption.AesEncryptUtil2;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @program: recruit-boot-parent
 * @author: JD
 * @create: 2020-10-30 10:52
 **/
public class PasswordLogin extends AbstractLogin<SysUser> {

    @Autowired
    private ISysUserService userService;

    private final LoginVO vo;

    @Autowired
    private IPiInfoService piInfoService;

    @Autowired
    private IEnInfoService enInfoService;

    @Autowired
    ISysUserDepartService userDepartService;

    public PasswordLogin(String username, String password, LoginVO vo) {
        super(username, password);
        this.vo = vo;
    }

    @Override
    public Object login() {
        SysUser member = userService.lambdaQuery().eq(SysUser::getPhone, username).one();
        if (member == null) {
            return null;
        } else {
            String password = this.password;
            try {
                password = AesEncryptUtil2.decrypt(password);
            } catch (Exception e) {
                Assert.isTrue(false, "账号或密码错误");
            }

            String encrypt = PasswordUtil.encrypt(member.getUsername(), password, member.getSalt());
            String code = member.getPassword();
            Assert.isTrue(StrUtil.equals(encrypt, code), "账号或密码错误");

            String memberId = member.getId();
            PiInfo piInfo = piInfoService.lambdaQuery()
                .eq(PiInfo::getUserId, memberId)
                .eq(PiInfo::getDelFlag, false)
                .one();
            Assert.isTrue(null != piInfo, "登录失败，请使用短信验证码登录");
            vo.setId(piInfo.getUserId());
            userService.updateById(member);

            List<SysUserDepart> count = userDepartService.lambdaQuery().eq(SysUserDepart::getUserId, vo.getId()).list();
            if (CollUtil.isEmpty(count)) {
                vo.setReg(false);
            } else {
                String id = count.get(0).getDepId();
                int departInfo = enInfoService.lambdaQuery().eq(EnInfo::getId, id).eq(EnInfo::getDelFlag, false).count();
                vo.setReg(departInfo != 0);
            }

            Boolean banFlag = piInfo.getBanFlag();
            Assert.isFalse(null != banFlag && banFlag, "当前账号已被禁用，请联系相关客服进行处理");
            member.setBanFlag(piInfo.getBanFlag());
            return member;
        }
    }

    @Override
    public void success(SysUser user) {
        vo.setId(user.getId());
        vo.setMobile(user.getPhone());
        vo.setUsername(user.getUsername());
        vo.setVip(true);
        vo.setDesc("");
        vo.setToken(super.token);
        user.setToken(super.token);
        userService.updateById(user);
    }
}
